﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using DataTools.Memory;

namespace DataTools.Interop
{

    /// <summary>
    /// Miscellaneous utility functions that do not belong anywhere else.
    /// </summary>
    public static class Util
    {

        /// <summary>
        /// Converts two UINTs into a long
        /// </summary>
        /// <param name="low"></param>
        /// <param name="high"></param>
        /// <returns></returns>
        public static long MakeLong(uint low, uint high)
        {
            return (long)((ulong)low | ((ulong)high) << 32);
        }

        [DllImport("devmgr.dll", CharSet = CharSet.Unicode, EntryPoint = "DeviceProperties_RunDLLW")]
        internal static extern bool DeviceProperties_RunDLL(IntPtr hwnd, IntPtr hAppInstance, string cmdLine, int nCmdShow);

    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct FILETIME
    {
        private uint lo;
        private uint hi;

        public static implicit operator DateTime(FILETIME v)
        {
            SYSTEM_TIME st = new SYSTEM_TIME();
            FILETIME lft = new FILETIME();

            SYSTEM_TIME.FileTimeToLocalFileTime(ref v, ref lft);
            SYSTEM_TIME.FileTimeToSystemTime(ref lft, ref st);

            return new DateTime(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
        }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct SYSTEM_TIME
    {
        [DllImport("kernel32.dll")]
        internal static extern int FileTimeToLocalFileTime(ref FILETIME ft, ref FILETIME lft);

        [DllImport("kernel32.dll")]
        internal static extern int FileTimeToSystemTime(ref FILETIME ft, ref SYSTEM_TIME st);

        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;
    };


}
